//
//  TabBarController.m
//  XiXi
//
//  Created by HawkingWang on 16/9/29.
//  Copyright © 2016年 xi-xi.cc. All rights reserved.
//

#import "TabBarController.h"

#import <QuartzCore/QuartzCore.h>

#import "DataViewController.h"//检票查看Controller
#import "OnlineRepertoireViewController.h"//在线剧目Controller
#import "ScanCodeViewController.h"//扫描二维码Controller

@interface TabBarController () <UITabBarControllerDelegate>

@property (nonatomic,strong)UIButton *button;

@end

@implementation TabBarController
{
    UINavigationController *_scanNav;
}
@synthesize button;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTabBarVC];
    [self setup];
    [self addButtonNotifation];
    self.tabBar.barTintColor = [UIColor whiteColor];
}

// 初始化所有子控制器
- (void)setTabBarVC{
    [self setTabBarChildController:[[OnlineRepertoireViewController alloc] init] title:@"在线剧目" image:@"online" selectImage:@"online_touch"];
    
    [self setTabBarChildController:[[ScanCodeViewController alloc] init] title:@"" image:@"" selectImage:@""];
    
    [self setTabBarChildController:[[ScanCodeViewController alloc] init] title:@"1" image:@"" selectImage:@""];
    
    [self setTabBarChildController:[[DataViewController alloc] init] title:@"检票查看" image:@"data" selectImage:@"data_touch"];
}


// 添加tabbar的子viewcontroller
- (void)setTabBarChildController:(UIViewController*)controller title:(NSString*)title image:(NSString*)imageStr selectImage:(NSString*)selectImageStr{
    
    UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:controller];
    nav.navigationBar.barTintColor= RGBCOLOR(34, 36, 45);
    nav.navigationBar.tintColor = [UIColor whiteColor];
    nav.navigationBar.translucent = NO;
    UIImageView *logo = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 64) / 2, 7, 64, 30)];
    logo.image = [UIImage imageNamed:@"logo"];
    [nav.navigationBar addSubview:logo];
    
    
    //渐变View
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 44, SCREEN_WIDTH, 4)];
    UIColor *color1 = RGBCOLOR(33, 171, 246);
    UIColor *color2 = RGBCOLOR(247, 38, 38);
    CAGradientLayer *gradientLayer = [[CAGradientLayer alloc] init];
    gradientLayer.colors = @[(__bridge id)color1.CGColor,(__bridge id)color2.CGColor];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 0);
    gradientLayer.frame = CGRectMake(0, 0, SCREEN_WIDTH, 4);
    [view.layer insertSublayer:gradientLayer atIndex:0];
    [nav.navigationBar addSubview:view];
    
  
    
    nav.tabBarItem.title = title;
    
    nav.tabBarItem.image = [[UIImage imageNamed:imageStr]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    nav.tabBarItem.selectedImage = [[UIImage imageNamed:selectImageStr]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    [nav.tabBarItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"PingFang SC" size:10],NSForegroundColorAttributeName:RGBA(184, 178, 172, 1.0)} forState:UIControlStateNormal];
    [nav.tabBarItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"PingFang SC" size:10],NSForegroundColorAttributeName:RGBA(57, 59, 67, 1.0)} forState:UIControlStateSelected];
    
  
    if ([title isEqualToString:@"1"]) {
     nav.tabBarItem.title = @"";
        
        
    }
    if ([title isEqualToString:@""]) {
        _scanNav = nav;
        
        return;
    }
    [self addChildViewController:nav];
}

-(void)setup
{
    //  添加突出按钮
    [self addCenterButtonWithImage:[UIImage imageNamed:@"scan"] selectedImage:[UIImage imageNamed:@"scan_touch"]];
    //  UITabBarControllerDelegate 指定为自己
    self.delegate=self;
    //  指定当前页——中间页
    //self.selectedIndex=0;
    //  设点button状态
    //button.selected=YES;
    //  设定其他item点击选中颜色
    
}

#pragma mark === addCenterButton ===
-(void) addCenterButtonWithImage:(UIImage*)buttonImage selectedImage:(UIImage*)selectedImage
{
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:self action:@selector(presentScan) forControlEvents:UIControlEventTouchUpInside];
    button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
    
    //  设定button大小为适应图片
    button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
    [button setImage:buttonImage forState:UIControlStateNormal];
    [button setImage:selectedImage forState:UIControlStateHighlighted];
//    [button setBackgroundColor:[UIColor redColor]];
    //  去掉选中button时候的阴影
    button.adjustsImageWhenHighlighted=NO;
    /*
     *  核心代码：设置button的center 和 tabBar的 center 做对齐操作， 同时做出相对的上浮
     */
    CGPoint center = self.tabBar.center;
    center.y = center.y - 15;
    button.center = center;
    [self.view addSubview:button];
    [self.view bringSubviewToFront:button];
    
    
    // try
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:self action:@selector(presentScan) forControlEvents:UIControlEventTouchUpInside];
    btn.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
    btn.frame = CGRectMake(0.0, 0.0, SCREEN_WIDTH / 3, buttonImage.size.height );
    [btn setBackgroundColor:[UIColor clearColor]];
    btn.adjustsImageWhenHighlighted=NO;

    CGPoint centera = self.tabBar.center;
    btn.center = centera;
    [self.view addSubview:btn];
    [self.view bringSubviewToFront:btn];

    
    
}

- (void)presentScan{
      [button setImage:[UIImage imageNamed:@"scan_touch"] forState:UIControlStateNormal];
    [self presentViewController:_scanNav animated:YES completion:nil];
    
    
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [button setImage:[UIImage imageNamed:@"scan"] forState:UIControlStateNormal];
        });

}

#pragma mark- TabBar Delegate
//添加大圆按钮的通知
-(void)addButtonNotifation{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(buttonHidden) name:@"buttonNotifationCenter" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(buttonNotHidden) name:@"buttonNotHidden" object:nil];
}
-(void)buttonNotHidden{
    button.hidden=NO;
}
-(void)buttonHidden{
    button.hidden=YES;
}








@end
